package cn.edu.hit.education.shiro;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hit.education.pojo.Permission;
import cn.edu.hit.education.pojo.Role;
import cn.edu.hit.education.pojo.User;
import cn.edu.hit.education.service.IPermissonService;
import cn.edu.hit.education.service.IRolePermissonService;
import cn.edu.hit.education.service.IRoleService;
import cn.edu.hit.education.service.IUserRoleService;
import cn.edu.hit.education.service.IUserService;
/**
 * 
* <p>Title: UserRealm</p>  
* <p>Description: 进行了登录的密码认证和授权 </p>  
* @author 罗成
* @date 2021年5月4日
 */
public class UserRealm extends AuthorizingRealm{
	@Autowired
	IUserService userService;
	
	@Autowired
	IUserRoleService userRoleService;
	
	@Autowired
	IRoleService roleService;
	
	@Autowired
	IRolePermissonService rolePermissonService;
	
	@Autowired
	IPermissonService permissonService;
	
	

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		 //1从主体传过来的授权信息中获取用户名
	    User user1 = (User) principalCollection.getPrimaryPrincipal();
	    //2.通过用户名来获取数据库或者缓存中的角色数据
	    User user = userService.queryUserByUsername(user1.getUsername());
	    int roleId = userRoleService.queryRoleIdByUser(user.getId());
	    Role role = roleService.queryById(roleId);
	    //3.通过用户名来获取数据库或者缓存中的权限数据
	    int permissionId = rolePermissonService.queryPerIdByRoleId(role.getId());
	    Permission permission = permissonService.queryPermissionById(permissionId);
	    
	    Set<String> pers = new HashSet<String>();
	    pers.add(permission.getPermission());
	    Set<String> roles = new HashSet<String>();
	    roles.add(role.getRole());

	    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
	    simpleAuthorizationInfo.setStringPermissions(pers);
	    simpleAuthorizationInfo.setRoles(roles);

		return simpleAuthorizationInfo;

	}
	
	
	/**
	 * 认证方法，采用MD5进行1024次加密，盐值为用户名
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		
		System.out.println("进入了realm!");
		String username = (String) token.getPrincipal();
		System.out.println(username);
		User user = userService.queryUserByUsername(username);
		if(user  == null){
			return null;
		}
		
		//设置颜值
		ByteSource salt = ByteSource.Util.bytes(user.getSalt());
		
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,user.getPassword(),salt,"UserRealm");
	
		return authenticationInfo;
		
	}

}
